package com.permissions.dispatcher.sample;


import com.permissions.dispatcher.annotation.*;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;
import ohos.utils.PacMap;

@RuntimePermissions
public class MainAbility extends Ability {

    public static final String CAMERA_PERMISSION_NAME ="ohos.permission.CAMERA";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        //打开注释可以测试slice中的权限申请
//        super.setMainRoute(MainAbilitySlice.class.getName());
          super.setUIContent(ResourceTable.Layout_ability_main);
//        addActionRoute("ability.main.slice",MainAbilitySlice.class.getName());

        Button buttonStart = (Button) findComponentById(ResourceTable.Id_btn_main_apply);
        buttonStart.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //studio原因报红，但是也是能正常运行的不要慌，在对应entry->build->generated->sources文件夹上右键选择Mark Directory as ->选择 Generated Source Root。
                com.permissions.dispatcher.sample.MainAbilityPermissionsDispatcher.toOpenPermissionWithPermissionCheck(MainAbility.this);
            }
        });

        Button buttonJudge = (Button) findComponentById(ResourceTable.Id_btn_main_judge);
        buttonJudge.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                judgePermission();
            }
        });

        Button buttonThree = (Button) findComponentById(ResourceTable.Id_btn_main_more);
        buttonThree.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                intentToNextPage("com.permissions.dispatcher.sample.TestPermissionAbility");
            }
        });
    }

    @NeedsPermission(CAMERA_PERMISSION_NAME)
    public void toOpenPermission() {
        ToastUtil("权限已申请成功");
    }

    @OnPermissionDenied(CAMERA_PERMISSION_NAME)
    public void toResetPermission() {
        ToastUtil("OnPermissionDenied权限被拒绝了");
    }

    @OnShowRationale(CAMERA_PERMISSION_NAME)
    public void showDialog(PermissionRequest request) {
        ToastUtil("showDialog解释你需要你打开相应权限原因,需要写dialog弹框选择");
        //确定申请
        request.proceed();
        //取消
//        request.cancel();
    }

    @OnNeverAskAgain(CAMERA_PERMISSION_NAME)
    public void toExplainPermission() {
        ToastUtil("OnNeverAskAgain解释这个权限为什么被拒绝");
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
    }

    /**
     * 是否相应权限
     */
    public void judgePermission() {
        if (verifySelfPermission(CAMERA_PERMISSION_NAME) != IBundleManager.PERMISSION_GRANTED) {
            ToastUtil("还没有获取相机权限");
        }else {
            ToastUtil("已经获取了相机权限");
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        com.permissions.dispatcher.sample.MainAbilityPermissionsDispatcher.onRequestPermissionsFromUserResult(this,requestCode,grantResults);
    }

    public void intentToNextPage(String i) {
        Intent intent = new Intent();
        Operation operationBuilder = new Intent.OperationBuilder()
                .withAbilityName(i)
                .withDeviceId("")
                .withBundleName(getBundleName())
                .build();
        intent.setOperation(operationBuilder);
        startAbility(intent);
    }

    public void intentToNext(String i) {
        Intent intent = new Intent();
        Operation operationBuilder = new Intent.OperationBuilder()
                .withAbilityName(i)
                .withDeviceId("")
                .withBundleName("com.permissions.dispatcher.sample.slice")
                .build();

        intent.setOperation(operationBuilder);
        startAbility(intent);
    }



    public void ToastUtil(String i) {
        ToastDialog dialog = new ToastDialog(getContext());
        dialog.setContentText(i);
        dialog.setDuration(2000);
        dialog.show();
    }
}
